package com.example.day02.service.Imp;

import com.example.day02.mapper.AccountMapper;
import com.example.day02.mapper.BookMapper;
import com.example.day02.service.PucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.dc.pr.PRError;

@Service
public class PucherServiceImp implements PucherService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private AccountMapper accountMapper;
//(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    //事务传播行为
    //Propagation.REQUIRED  如果当前环境中存在事务，则加入，没有则开启
    @Transactional(propagation = Propagation.REQUIRED)         //默认的，不写也没事

//    @Transactional(propagation = Propagation.REQUIRES_NEW)   //无论线程是否存储事务，都会开启一个事务
    @Override
    public void pucherGoods(String usernmae, Integer id) {

        double price = bookMapper.getPrice(id);
        bookMapper.update(id);
//        System.out.println(10/0);
        try {
            if (id==2){
                System.out.println(10/0);
            }
        } catch (Exception e) {
//            e.printStackTrace();

             throw  e;
        }


        int update = accountMapper.update(usernmae, price);

        System.out.println(update);
    }
}
